<?php
/**
 * Created by PhpStorm.
 * User: liyuanpeng
 * Date: 08/08/14
 * Time: 14:46
 */


defined('IN_IA') or exit('Access Denied');
$table = 'send_group';
$dos = array('display', 'post', 'list', 'del', 'query');
$do = !empty($_GPC['do']) && in_array($do, $dos) ? $do : 'list';

if ($do == 'list') {
    $_W['page']['title'] = '管理用户组模板 - 模板列表';
    $status = getGeneStatus();
    $keyword = trim($_GPC['names']);
    $wheresql = " WHERE 1 ";
    if (!empty($keyword)) {
        $wheresql .= " AND title LIKE '%{$keyword}%'";
        $_GET['names'] = $_GPC['names'];
    }

    $pindex = max(1, intval($_GPC['page']));
    $psize = 20; //ORDER BY createtime DESC
    $list = pdo_fetchall("SELECT * FROM " . tablename($table) . $wheresql . '   LIMIT ' . ($pindex - 1) * $psize . ',' . $psize);
    $pager = '';
    template('sms/usergroup_list');
}
if ($do == 'post') {

    $id = intval($_GPC['id']);
    if ($id) {
        $_W['page']['title'] = '管理接收用户组 - 修改用户组';
    } else {
        $_W['page']['title'] = '管理接收用户组 - 增加用户组';

    }
    if (checksubmit('submit')) {
        $data = array(
            'title' => $_GPC['title'],
        );
        if (count($_GPC['ids']) > 0) {
            $sendee = getSendeeByIds($_GPC['ids']);
            $data['sendee'] = json_encode($sendee);
        } else {
            message('请增加短信接收人信息！');
        }
        // 选择短信接受项
        if (count($_GPC['msgids']) > 0) {
            $msg = getMsgByIds($_GPC['msgids']);
            $data['items'] = json_encode($msg);
        } else {
            message('请增加短信信息！');
        }
        if (isset($_GPC['status'])) {
            $data['status'] = $_GPC['status'];
        }
        if (empty($id)) {
            $data['createtime'] = TIMESTAMP;
            pdo_insert($table, $data);
        } else {
            $row = pdo_fetch("SELECT * FROM " . tablename($table) . " WHERE   id = '{$id}'");
            pdo_update($table, $data, array('id' => $id));
        }
        message('数据更新成功！', url('sms/usergroup/list'), 'success');
    }
    if (!empty($id)) {
        $row = pdo_fetch("SELECT * FROM " . tablename($table) . " WHERE   id = {$id} ");
        $msgs = empty($row['items']) ? array() : json_decode($row['items'], true);
        $selects = empty($row['sendee']) ? array() : json_decode($row['sendee'], true);
    }
    template('sms/usergroup_post');
}
if ($do == 'del') {
    $id = intval($_GPC['id']);
    if ($id > 0) {
        pdo_delete($table, array('id' => $id));
        message('数据删除成功！', url('sms/usergroup/list'), 'success');
    }
}